package org.zcn.xmlg_backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.zcn.xmlg_backend.entity.Dept;
import org.zcn.xmlg_backend.entity.Result;
import org.zcn.xmlg_backend.service.IDeptService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 张灿能
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    //查询部门信息
    @GetMapping()
    public Result<List<Dept>> list(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }


    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        boolean save = deptService.save(dept);
        if (save) {
            return Result.success();
        } else {
            return Result.error("添加部门失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable("id") Long id) {
        boolean remove = deptService.removeById(id);
        if (remove) {
            return Result.success();
        } else {
            return Result.error("删除部门失败");
        }
    }
}
